package com.medicine.hbp.fragment;

import java.util.List;

import org.kymjs.kjframe.http.HttpParams;

import com.medicine.hbp.adapter.ClassificationAdapter;
import com.medicine.hbp.adapter.ImageFragmentAdapter;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.AdlnfoListVo;
import com.medicine.hbp.mob.AdlnfoVo;
import com.medicine.hbp.mob.laberInfoListVo;
import com.medicine.hbp.mob.laberInfoVo;
import com.medicine.hbp.ui.ClassificationDetailActivity;
import com.medicine.hbp.ui.MainActivity;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author jindandan
 */
public class ClassificationFragment extends Fragment implements OnItemClickListener, IXListViewListener {
	private View View;
	private XListView xlistview;
	private ClassificationAdapter adapter;
	private ImageFragmentAdapter mAdapter;
	private ViewPager pager;

	private volatile boolean refresh = true;
	private volatile int page = 1;// ��ǰҳ����0��ʼ��
	private int pageSize = 10;
	private List<laberInfoVo> laberInfoVos;

	@Override
	public android.view.View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View = inflater.inflate(R.layout.fragment_classification, container, false);
		xlistview = (XListView) View.findViewById(R.id.xListView);
		initView();
		return View;
	}
	private void initView() {
//		xlistview.setLayoutAnimation(ListViewAnim.getListViewAnim());
		adapter = new ClassificationAdapter(getActivity());
		xlistview.setAdapter(adapter);
		xlistview.setOnItemClickListener(this);
		xlistview.setXListViewListener(this);
		httpBanner();
		httpLaberGroupList();
	}

	/**
	 */
	private void httpBanner() {
		HttpParams params = new HttpParams();
		params.put("userToken", CommUtil.getIMEI(getActivity()));
		AppHttps.HttpPost(getActivity(),HttpInterface.ADLIST, params,
				new AppHttpCallBack<AdlnfoListVo>(AdlnfoListVo.class, getActivity()) {
					@Override
					public void onSuccess(AdlnfoListVo result) {
						initViewPage(result.adInfo);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}

	/**
	 */
	private void httpLaberGroupList() {
		HttpParams params = new HttpParams();
		params.put("current", page);//
		params.put("rowCount", pageSize);//
		AppHttps.HttpPost(getActivity(),HttpInterface.LABER_GROUP_LIST, params,new AppHttpCallBack<laberInfoListVo>(laberInfoListVo.class, getActivity()) {
					@Override
					public void onSuccess(laberInfoListVo result) {
						boolean hasMore = true;
						laberInfoVos = result.laberInfo;
						if (refresh) {
							adapter.changeData(result.laberInfo);
						} else {
							adapter.addItems(result.laberInfo);
							adapter.notifyDataSetChanged();
						}
						if (result.laberInfo.size() < pageSize) {
							hasMore = false;
						}
						xlistview.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
						xlistview.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});
	}

	/**
	 * ����bannerչʾ
	 */
	private void initViewPage(List<AdlnfoVo> adInfo) {
		View v = LayoutInflater.from(getActivity()).inflate(R.layout.banner_classification, null);
		mAdapter = new ImageFragmentAdapter(getActivity().getSupportFragmentManager());
		pager = (ViewPager) v.findViewById(R.id.pager);
		pager.setAdapter(mAdapter);
		mAdapter.setData(adInfo);
		xlistview.addHeaderView(v);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, android.view.View arg1, int arg2, long arg3) {
		ToastUtil.showShort(getActivity(), arg2 + "");
		List<laberInfoVo> laberInfoVos=adapter.getItems();
		Intent intent = new Intent(getActivity(), ClassificationDetailActivity.class);
		intent.putExtra("ID", laberInfoVos.get(arg2 - 2).laberId);
		intent.putExtra("NAME", laberInfoVos.get(arg2 - 2).laberName);
		intent.putExtra("IMG", laberInfoVos.get(arg2 - 2).laberImg);
		getActivity().startActivity(intent);

	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		xlistview.setPullLoadEnable(false);
		httpLaberGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpLaberGroupList();
	}
}
